import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/manager/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {path: '/login', name: 'login', component: () => import('../views/Login.vue')},
  {path: '/register', name: 'register', component: () => import('../views/Register.vue')},
  {path: '/manager', name: 'manager', component: () => import(/* webpackChunkName: "about" */ '../views/ManagerLayout.vue'),
      children:[
        {path: 'workplace', component:() => import('../views/manager/Workplace.vue')},
        {path: 'message', component:() => import('../views/manager/Message.vue')},
        {path: 'data', component:() => import('../views/manager/Data.vue')},
        {path: 'goods', component:() => import('../views/manager/Goods.vue')},
        {path: 'combo', component:() => import('../views/manager/Combo.vue')},
        {path: 'clazz', component:() => import('../views/manager/Clazz.vue')},
        {path: 'order_of', component:() => import('../views/manager/Order_of.vue')},
        {path: 'admin', component:() => import('../views/manager/Admin.vue')},
        {path: 'user', component:() => import('../views/manager/User.vue')},
        {path: 'userInfo', component:() => import('../views/manager/UserInfo.vue')},
        {path: 'system', component:() => import('../views/manager/System.vue')},
        {path: 'shenhe', component:() => import('../views/manager/Shenhe.vue')},
        {path: 'news', component:() => import('../views/manager/News.vue')},
        {path: 'addressbook', component:() => import('../views/manager/Addressbook.vue')},

      ]
  },


  {
    path: '/about', name: 'about', component: () => import(/* webpackChunkName: "about" */ '../views/manager/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由守卫
router.beforeEach((to ,from, next) => {
  if (to.path === '/') {
    return next("/login");
  }
  let user = localStorage.getItem("user");
  if (!user && to.path !== '/login' && to.path !== '/register' && to.path !== '/front/index') {
    return next("/login");
  }
  next();
})

export default router
